package com.ruoyi.mushcoin.controller;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.mushcoin.domain.vo.TgUserLoginLogVo;
import com.ruoyi.mushcoin.domain.vo.TgUserLoginLogImportVo;
import com.ruoyi.mushcoin.domain.bo.TgUserLoginLogBo;
import com.ruoyi.mushcoin.listener.TgUserLoginLogImportListener;
import com.ruoyi.mushcoin.service.ITgUserLoginLogService;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.orm.core.page.TableDataInfo;

/**
 * TG游戏登录日志Controller
 *
 * @author billy
 * 2024-10-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/userLoginLog")
public class TgUserLoginLogController extends BaseController
{
    @Resource
    private ITgUserLoginLogService tgUserLoginLogService;

    /**
     * 查询TG游戏登录日志列表
     */
    @SaCheckPermission("mushcoin:userLoginLog:list")
    @GetMapping("/list")
    public TableDataInfo<TgUserLoginLogVo> list(TgUserLoginLogBo tgUserLoginLogBo)
    {
        return tgUserLoginLogService.selectPage(tgUserLoginLogBo);
    }

    /**
     * 导出TG游戏登录日志列表
     */
    @SaCheckPermission("mushcoin:userLoginLog:export")
    @Log(title = "TG游戏登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgUserLoginLogBo tgUserLoginLogBo)
    {
        List<TgUserLoginLogVo> list = tgUserLoginLogService.selectList(tgUserLoginLogBo);
        ExcelUtil.exportExcel(list, "TG游戏登录日志", TgUserLoginLogVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG游戏登录日志", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:userLoginLog:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgUserLoginLogImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgUserLoginLogImportVo.class, new TgUserLoginLogImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:userLoginLog:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG游戏登录日志", TgUserLoginLogImportVo.class, response);
    }

    /**
     * 获取TG游戏登录日志详细信息
     */
    @SaCheckPermission("mushcoin:userLoginLog:query")
    @GetMapping(value = "/{id}")
    public R<TgUserLoginLogVo> getInfo(@PathVariable Long id)
    {
        return R.ok(tgUserLoginLogService.selectById(id));
    }

    /**
     * 新增TG游戏登录日志
     */
    @SaCheckPermission("mushcoin:userLoginLog:add")
    @Log(title = "TG游戏登录日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgUserLoginLogBo tgUserLoginLogBo)
    {
        boolean inserted = tgUserLoginLogService.insert(tgUserLoginLogBo);
        if (!inserted) {
            return R.fail("新增TG游戏登录日志记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG游戏登录日志
     */
    @SaCheckPermission("mushcoin:userLoginLog:edit")
    @Log(title = "TG游戏登录日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgUserLoginLogBo tgUserLoginLogBo)
    {
        Boolean updated = tgUserLoginLogService.update(tgUserLoginLogBo);
        if (!updated) {
            return R.fail("修改TG游戏登录日志记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG游戏登录日志
     */
    @SaCheckPermission("mushcoin:userLoginLog:remove")
    @Log(title = "TG游戏登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids)
    {
        boolean deleted = tgUserLoginLogService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG游戏登录日志记录失败!");
        }
        return R.ok();
    }
}
